package com.ruoyi.ocr;

import com.ruoyi.ocr.domain.DrivingLicenseOcrResult;
import com.ruoyi.ocr.domain.IdCardOcrResult;
import com.ruoyi.ocr.service.OcrService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * OCR识别控制器
 *
 * @author example
 */
@RestController
@RequestMapping("/api/ocr")
public class OcrController {

    private final OcrService ocrService;

    public OcrController(OcrService ocrService) {
        this.ocrService = ocrService;
    }

    /**
     * 身份证识别接口
     */
    @PostMapping("/id-card")
    public ResponseEntity<IdCardOcrResult> idCardOcr(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }

        IdCardOcrResult result = ocrService.idCardOcr(file);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    /**
     * 驾驶证识别接口
     */
    @PostMapping("/driving-license")
    public ResponseEntity<DrivingLicenseOcrResult> drivingLicenseOcr(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }

        DrivingLicenseOcrResult result = ocrService.drivingLicenseOcr(file);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    /**
     * 根据任务ID查询身份证识别结果
     */
    @GetMapping("/id-card/result/{taskId}")
    public ResponseEntity<IdCardOcrResult> getIdCardResult(@PathVariable Long taskId) {
        IdCardOcrResult result = ocrService.getIdCardResultByTaskId(taskId);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    /**
     * 根据任务ID查询驾驶证识别结果
     */
    @GetMapping("/driving-license/result/{taskId}")
    public ResponseEntity<DrivingLicenseOcrResult> getDrivingLicenseResult(@PathVariable Long taskId) {
        DrivingLicenseOcrResult result = ocrService.getDrivingLicenseResultByTaskId(taskId);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
}
